//
//  HomeTableViewController.swift
//  SinaWeibo
//
//  Created by liyue on 15/11/8.
//  Copyright © 2015年 李越. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellID = "HomeCellID"

class HomeTableViewController: BaseTableViewController {

    private lazy var statuses = [Status]()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
        visitorLoginView?.setUIInfo(nil, title: "关注一些人,回这里看看有什么惊喜")
         
            return
        }

        prepareTableView()
        
        loadData()
    }
    
    private func prepareTableView() {
        
        //设置下拉刷新
        refreshControl = UIRefreshControl()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        //注册Cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellID)
        
        //自动计算行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = .None
        
        tableView.tableFooterView = indicatorView
//        tableView.rowHeight = 200
    }
    
    //加载微博首页数据
    
    @objc private func loadData() {
        
        var since_id = 0
        var max_id = 0
        
        if indicatorView.isAnimating() {
            
            since_id = 0
            max_id = statuses.last?.id ?? 0
        } else {
            
            max_id = 0
            since_id = statuses.first?.id ?? 0
        }
        
        StatusListViewModel.loadData(since_id: since_id, max_id: max_id) { (array) -> () in
            
            self.refreshControl?.endRefreshing()
            if array == nil {
                
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0 {
                
                self.statuses = array! + self.statuses
            } else if max_id > 0 {
                self.statuses += array!
                self.indicatorView.stopAnimating()
            } else {
                self.statuses = array!
            }
            
            //刷新表格
            self.tableView.reloadData()
        }
        
//        StatusListViewModel.loadData { (array) -> () in
//            
//            self.refreshControl?.endRefreshing()
//            if array == nil {
//                
//                SVProgressHUD.showErrorWithStatus(AppErrorTip)
//                return
//            }
//            //刷新表格
//            self.statuses = array!
//            self.tableView.reloadData()
//        }
    }
    
    private lazy var indicatorView: UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indicator
    }()

}

extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! StatusCell
//        cell.textLabel?.text = statuses[indexPath.row].text
//        cell.textLabel?.text = statuses[indexPath.row].user?.name
        cell.status = statuses[indexPath.row]
        
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating() {
            
            indicatorView.startAnimating()
            loadData()
        }
        
        return cell
    }
}
